﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace SharpDepend.Datatypes
{
    /// <summary>
    /// This class is a list with structs. Methods Clear(), AddRange() and ToArray() is very fast.
    /// <para>ToArray() do not copy the array, it just share the internally array but can contains<br/>
    /// crap data after the Count so remeber to not use ToArray().Length!</para>
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public sealed class FastStructList<T> : IList<T> where T : struct
    {
        /// <summary>
        /// Fast data access. NOTE: This is an usafe usage. Use it on your own risk.
        /// </summary>
        public T[] mInternalList;

        private int mInternalListLength;

        /// <summary>
        /// Create an instance with capacity of 1024 elements.
        /// </summary>
        public FastStructList()
            : this(1024)
        {
        }

        public FastStructList(int capacity)
        {
            mInternalList = new T[capacity];
        }

        public T this[int index]
        {
            get
            {
                return mInternalList[index];
            }
            set
            {
                mInternalList[index] = value;
            }
        }

        public int Count
        {
            get
            {
                return mInternalListLength;
            }
        }

        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }

        public void Add(T item)
        {
            int totalLength = mInternalListLength + 1;
            if (totalLength > mInternalList.Length)
            {
                var temp = mInternalList;
                mInternalList = new T[totalLength * 2];
                Array.Copy(temp, 0, mInternalList, 0, temp.Length);
            }

            mInternalList[mInternalListLength] = item;
            mInternalListLength = totalLength;
        }

        public void AddRange(FastStructList<T> items)
        {
            AddRange(items.mInternalList, items.mInternalListLength);
        }

        public void AddRange(T[] items)
        {
            int totalLength = mInternalListLength + items.Length;
            if (totalLength > mInternalList.Length)
            {
                var temp = mInternalList;
                mInternalList = new T[totalLength * 2];
                Array.Copy(temp, 0, mInternalList, 0, temp.Length);
            }

            Array.Copy(items, 0, mInternalList, mInternalListLength, items.Length);
            mInternalListLength = totalLength;
        }

        public void AddRange(T[] items, int length)
        {
            int totalLength = mInternalListLength + length;
            if (totalLength > mInternalList.Length)
            {
                var temp = mInternalList;
                mInternalList = new T[totalLength * 2];
                Array.Copy(temp, 0, mInternalList, 0, temp.Length);
            }

            Array.Copy(items, 0, mInternalList, mInternalListLength, length);
            mInternalListLength = totalLength;
        }

        public void Clear()
        {
            //Array.Clear(mInternalList, 0, mInternalListLength);
            mInternalListLength = 0;
        }

        public bool Contains(T item)
        {
            throw new NotImplementedException();
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }

        public IEnumerator<T> GetEnumerator()
        {
            throw new NotImplementedException();
        }

        public int IndexOf(T item)
        {
            throw new NotImplementedException();
        }

        public void Insert(int index, T item)
        {
            throw new NotImplementedException();
        }

        public bool Remove(T item)
        {
            throw new NotImplementedException();
        }

        public void RemoveAt(int index)
        {
            for (int i = index; i < mInternalListLength; i++)
            {
                mInternalList[i] = mInternalList[i + 1];
            }
            --mInternalListLength;
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public void ResetLength(int length)
        {
            if (length > mInternalList.Length)
            {
                mInternalList = new T[length];
            }
            mInternalListLength = length;
        }

        public void SetLength(int length)
        {
            if (length > mInternalList.Length)
            {
                var temp = mInternalList;
                mInternalList = new T[length];
                Array.Copy(temp, 0, mInternalList, 0, temp.Length);
            }
            mInternalListLength = length;
        }

        public T[] ToArray()
        {
            return mInternalList;
        }

        public void RemoveRange(int offset, int length)
        {
            if (mInternalListLength - length < 0)
            {
                throw new IndexOutOfRangeException();
            }

            int removeLength = mInternalListLength - length;
            for (int i = offset; i < removeLength; i++)
            {
                mInternalList[i] = mInternalList[i + length];
            }
            mInternalListLength -= length;
        }
    }
}
